﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;

public partial class WebUserControl_CtrBaocaotuvanvienkhonghoatdong : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    static string error = "";
    static DataTable db;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getTTPP();
        }
        else
        {
            try
            {
                Title();
                ASPxPivotGrid1.DataSource = db;
                ASPxPivotGrid1.DataBind();
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }
    private void getTTPP()
    {
        dt=new ClassExecute.ClassExecute();
        DataTable TTPP = dt.Get_Data_DataTable(str, ref error, "[GET_SITENAME_ALL]", CommandType.StoredProcedure);
        dr_vung.DataSource = TTPP;
        dr_vung.DataValueField = "Sitecode";
        dr_vung.DataTextField = "sitename";
        dr_vung.DataBind();

    }
    protected void bt_baocao_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            db = dt.Get_Data_DataTable(str, ref error, "K_ReportTVVNotWork", CommandType.StoredProcedure,
                new SqlParameter("@username", Session["username"].ToString()),
                new SqlParameter("@Sitecode", dr_vung.SelectedValue.ToString()),
                new SqlParameter("@thang", Int32.Parse(DropThang.SelectedValue.ToString())));
            if (db.Rows.Count > 0)
            {
                ASPxPivotGrid1.Fields.Clear();
                Title();
                ASPxPivotGrid1.CustomizationFieldsLeft = 500;
                ASPxPivotGrid1.CustomizationFieldsTop = 500;
                ASPxPivotGrid1.OptionsDataField.AreaIndex = 0;

                foreach (DataColumn col in db.Columns)
                {

                    ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);

                }

                for (int j = 8; j < ASPxPivotGrid1.Fields.Count; j++)
                {
                    ASPxPivotGrid1.Fields[j].ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    ASPxPivotGrid1.Fields[j].ValueFormat.FormatString = "{0:###,###}";

                    ASPxPivotGrid1.Fields[j].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    ASPxPivotGrid1.Fields[j].CellFormat.FormatString = "{0:###,###}";
                }
                ASPxPivotGrid1.DataSource = db;
                ASPxPivotGrid1.DataBind();
            }
        }
        catch(Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    public void Title()
    {
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Baocao"]))
        {
            ASPxPivotGrid1.Fields.Add("Baocao", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Baocao"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Baocao"].Caption = "Báo cáo TVV không hoạt động";
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Time"]))
        {
            ASPxPivotGrid1.Fields.Add("Time", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Time"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Time"].Caption = "Ngày giờ lấy báo cáo : " + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString();
        }
    }
    protected void bt_export_Click(object sender, EventArgs e)
    {
        using (MemoryStream filestream = new MemoryStream())
        {

            string contentType = "application/ms-excel";
            string fileName = System.DateTime.Now.ToString("dd/MM/yyyy") + "_BaoCaoTVVKhongHoatDong.xls";
            ASPxPivotGridExporter1.ExportToXls(filestream);
            byte[] buffer = filestream.GetBuffer();
            Response.Clear();
            Response.Buffer = false;
            Response.AppendHeader("Content-Type", contentType);
            Response.AppendHeader("Content-Transfer-Encoding", "binary");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.BinaryWrite(buffer);
            Response.End();
        }
    }
}
